// package-signin/pages/detail/index.js
const app = getApp();
import { PACKAGE_SIGNIN_PAGE } from '../../../constants/route';
import { UserInfoUpdateMixins, ShareImageMixins, PageOssMixins, PageCommonFuncs } from '../../../behaviors/page';
import { navTo, showLoading, hideLoading, callPhone, tipToast, subscribeMessage } from '../../../common/env';
import { getPicFullPath } from '../../../common/utils';
import { MESSAGE_ID } from '../../../constants/message';
import { getSigninPeriod } from '../../biztools/signin';
import { getSigninInfo, doSignin } from '../../api/signin';
let loaded = false, brandId, showSuccess;

Page({
    mixins: [UserInfoUpdateMixins, ShareImageMixins, PageOssMixins, PageCommonFuncs],
    data: {
        awardList: [],
        signinInfo: {},
        procedureInfoInfo: {},
        statInfo: {},
        successModal: {
            show: false,
        },
        explainModal: {
            show: false,
        },
        ruleModal: {
            show: false,
        },
        awardSuccesModal: {
            show: false,
        },
    },
    onLoad(options) {
        brandId = options.brandId;
        showSuccess = options.showSuccess;
    },
    onShow() {
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                console.log('执行认证后回调函数, onShow');
                _.onShow(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            this._handleGetDetail();
        }
    },
    async _handleGetDetail() {
        !loaded && showLoading('加载中...');
        const { signinInfo, procedureInfo, statInfo } = await getSigninInfo(brandId);
        console.log('SigninDetail => 签到详情为', { signinInfo, procedureInfo });
        this.setData({
            awardList: getSigninPeriod(signinInfo.typeInfo),
            signinInfo,
            procedureInfo,
            statInfo,
            avatarUrl: getPicFullPath(this.data.userInfo.pic),
            'successModal.show': !!showSuccess,
        });
        showSuccess = false;
        if (!loaded) hideLoading(); loaded = true;
    },
    handleShowExplain() {
        this.setData({
            'explainModal.show': true,
        });
    },
    handleShowRule() {
        this.setData({
            'ruleModal.show': true,
        });
    },
    handleNavToRecord(e) {
        const { activityId } = this.data.signinInfo;
        navTo(PACKAGE_SIGNIN_PAGE['signinRecord'], {
            activityId,
        });
    },
    handleCallPhone(e) {
        const { tel } = this.data.signinInfo;
        callPhone(tel);
    },
    async handleSignin() {
        const { activityId, typeInfo } = this.data.signinInfo;
        const { continuousCheckInCount, isCheckInToday } = this.data.procedureInfo;
        if (isCheckInToday) return;
        // 订阅
        const message = await subscribeMessage([MESSAGE_ID.signin]);
        const flag = await doSignin({
            activityId,
            authorizeNextRemind: message && message.isAll,
        });
        if (flag) {
            if (continuousCheckInCount === typeInfo.continuous - 1) {
                this.setData({
                    'awardSuccesModal.show': true,
                });
            } else {
                tipToast(`连续签到${continuousCheckInCount + 1}天，星币 +${typeInfo.awardOnceCoins}`);
            }
            this._handleGetDetail();
        }
    },
})
